//
//  HomeArtistCollectionCell.m
//  jingdongfang
//
//  Created by LJL on 2018/5/7.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeArtistCollectionCell.h"
#import "HomeArtistCell.h"
#import "IntroViewController.h"
#import "IGHomeArtistApi.h"
#import "IGArtGalleryAttentApi.h"


@interface HomeArtistCollectionCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) IGArtGalleryAttentApi *attentApi;

@end


@implementation HomeArtistCollectionCell


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self addSubview:self.collectionView];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        [flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH,243) collectionViewLayout:flow];
        self.collectionView.backgroundColor = [UIColor c3Color];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.showsVerticalScrollIndicator = YES;//垂直方向
        self.collectionView.showsHorizontalScrollIndicator = NO;//水平方向
        [self.collectionView registerNib:[UINib nibWithNibName:@"HomeArtistCell" bundle:nil] forCellWithReuseIdentifier:@"HomeArtistCell"];
        self.attentApi = [IGArtGalleryAttentApi new];
    }
    return _collectionView;
}

- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr)
    {
        _dataArr = dataArr;
        [self.collectionView reloadData];
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count > 0 ? self.dataArr.count : 0;
//    return 10;
}

//设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(150, 213);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    @KWeakObj(self)
    HomeArtistCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeArtistCell" forIndexPath:indexPath];
    IGHomeArtist *artist = self.dataArr[indexPath.row];
    [cell.artistImg sd_setImageWithURL:[NSURL URLWithString:artist.image] placeholderImage:[UIImage imageNamed:@"place_head_Icon"]];
    NSDictionary *dic = artist.productBeanList[0];
    [cell.workImg sd_setImageWithURL:[NSURL URLWithString:dic[@"image"]] placeholderImage:[UIImage imageFromColor:[UIColor mainRandomColor]]];
    cell.nameL.text = artist.nike;
    cell.attcentBtn.selected = [artist.attentionState isEqualToString:@"0"] ? YES : NO;
    
    cell.block = ^(UIButton *btn) {
        [selfWeak attentDataRequest:[NSString stringWithFormat:@"%@", btn.selected ? @"1" : @"0"] dic:artist section:indexPath.row];
    };
    return cell;
}


- (void)attentDataRequest:(NSString *)operatorType dic:(IGHomeArtist *)dic section:(NSUInteger)section
{
    self.attentApi.operatorType = operatorType;
    self.attentApi.museumId = dic.aid;
    @KWeakObj(self)
    [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            NSString *str = [operatorType isEqualToString:@"0"] ? @"0" : @"1";
            dic.attentionState = str;
//            [_dataArr replaceObjectAtIndex:section withObject:dic];
            
            [UIView performWithoutAnimation:^{
                [selfWeak.collectionView reloadData];
            }];
            if ([operatorType isEqualToString:@"1"])
            {
            }
            else
            {
            }
        }
    }];
}


//设置分区边界
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 30, 10);
}

#pragma mark --- item 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IntroViewController *introVC = [[IntroViewController alloc] init];
    IGHomeArtist *art = self.dataArr[indexPath.row];
    introVC.u_id = art.aid;
    [UIViewController ig_pushViewController:introVC animated:YES];
}

@end
